/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.server;

import cn.ac.iie.di.compress.TarFilePath;
import cn.ac.iie.di.download.DownLoadFile;
import cn.ac.iie.di.global.GlobalParas;
import cn.ac.iie.di.splitor.FileListener;
import cn.ac.iie.di.splitor.FileRead;
import kill.KillHandler;
import org.apache.log4j.Logger;

/**
 *
 * @author Austin
 */
public class MediaDownloadStart {

    static private final Logger logger = Logger.getLogger(MediaDownloadStart.class.getName());

    public static void main(String[] args) {
        startFileSplitor();
        startDownload();
        startPackTar();
        startMonitor();
        KillHandler killhandle = new KillHandler();
        killhandle.registerSignal("TERM");
        logger.info("init the kill handler done ");
    }

    public static void startFileSplitor() {
        Thread listener = new Thread(new FileListener(GlobalParas.ORI_PATH, GlobalParas.FILE_NAME_QUEUE));
        listener.setName("FileListener");
        listener.start();
        logger.info("FileListener start ok");
        for (int i = 0; i < GlobalParas.READ_THREAD_SIZE; i++) {
            Thread read = new Thread(new FileRead(GlobalParas.DES_PATH, GlobalParas.FILE_NAME_QUEUE, GlobalParas.JSON_STR_QUEUE));
            read.setName("FileRead_" + i);
            read.start();
            logger.info("FileRead_" + i + " start ok");
        }
    }

    public static void startDownload() {
//        for (int i = 0; i < 1; i++) {
        for (int i = 0; i < GlobalParas.DW_THREAD_SIZE; i++) {
            Thread dwd = new Thread(new DownLoadFile(GlobalParas.JSON_STR_QUEUE, GlobalParas.JSON_TAR_QUEUE, i + ""));
            dwd.setName("DownLoadFile_" + i);
            dwd.start();
            logger.info("DownLoadFile_" + i + " start ok");
        }
    }

    public static void startPackTar() {
        for (int i = 0; i < GlobalParas.READ_THREAD_SIZE; i++) {
            Thread dwd = new Thread(new TarFilePath(GlobalParas.JSON_TAR_QUEUE, i + ""));
            dwd.setName("TarFilePath_" + i);
            dwd.start();
            logger.info("TarFilePath_" + i + " start ok");
        }
    }

    private static void startMonitor() {
        Thread dwd = new Thread(new Monitor());
        dwd.setName("Monitor");
        dwd.start();
        logger.info("Monitor start ok");
    }
}
